#define _CRT_SECURE_NO_WARNINGS 1


		《 Linux_19_等待子进程结束的过程，让父进程做一些其它事 》

基于函数指针级别的对于父进程完成任务进行结偶。
		< 回调方式 >

——————————————————————————————————————————————————————————————————————

		《 task.h 》
#pragma once
#include <stdio.h>
void PrintLog();
void Download();
void MysqlDataSync();

——————————————————————————————————————————————————————————————————————

		《 task.c 》		
#include "task.h"
void PrintLog()
{
    printf("begin PrintLog...\n");	//执行不同任务时只需要更改执行任务的内容：
}
void Download()
{
    printf("begin Download...\n");
}
void MysqlDataSync()
{
    printf("begin MySQLDataSync...\n");
}

——————————————————————————————————————————————————————————————————————

		《 Makefile 》
bin = myprocess
src = myprocess.c task.c

$(bin) :$(src)
@gcc - o $@ $ ^ -std = c99
.PHONY:clean
clean :
@rm - f $(bin)

——————————————————————————————————————————————————————————————————————

		《 myprocess 》
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "task.h"

typedef void(*func_t)();
#define N 3
func_t task[N] = { NULL };

void LoadTask()
{
    task[0] = PrintLog;
    task[1] = Download;
    task[2] = MysqlDataSync;
}
void HandlerTask()
{
    for (int i = 0; i < N; i++)
    {
        task[i]();
    }
}
//father
void DoOtherThing()
{
    HandlerTask();
}
void childRun()
{
    //int *p = NULL;
    int cnt = 5;
    while (cnt)
    {
        printf("I am child process, pid: %d, ppid:%d, cnt: %d\n", getpid(), getppid(), cnt);
        sleep(2);
        //usleep(10000);
        cnt--;
        //*p = 100;
    }
}

int main()
{
    printf("I am father, pid: %d, ppid:%d\n", getpid(), getppid());
    pid_t id = fork();
    if (id == 0)
    {
        // child
        childRun();
        printf("child quit ...\n");
        exit(123);
    }
    //sleep(10);
    LoadTask();
    // fahter
    while (1)
    {
        int status = 0;
        pid_t rid = waitpid(id, &status, WNOHANG);      // non block
        if (rid == 0)
        {
            sleep(1);
            //usleep(1000);
            printf("child is running, father check next time!\n");
            DoOtherThing();

        }
        else if (rid > 0)
        {
            if (WIFEXITED(status))
            {
                printf("child quit successs, exit code: %d\n", WEXITSTATUS(status));
            }
            else
            {
                printf("child quit unnormal!\n");
            }
            break;
        }
        else
        {
            printf("wait failed!\n");
            break;
        }
    }

    return 0;
}


——————————————————————————————————————————————————————————————————————


结果：
 ......
I am child process, pid: 3914, ppid : 3913, cnt : 2
child is running, father check next time! 
begin PrintLog...			// 在等待子进程期间,父进程做了其他的任务。
begin Download...
begin MySQLDataSync...

child is running, father check next time!
begin PrintLog...
begin Download...
begin MySQLDataSync...
I am child process, pid: 3914, ppid : 3913, cnt : 1
child is running, father check next time!
begin PrintLog...
begin Download...
begin MySQLDataSync...
child is running, father check next time!
begin PrintLog...
begin Download...
begin MySQLDataSync...
child quit ...
child is running, father check next time!
begin PrintLog...
begin Download...
begin MySQLDataSync...
child quit successs, exit code : 123
